#include "widget.h"
#include "ui_widget.h"
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QString>
#include <math.h>
#include <qmath.h>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    str1="0";
    str2="0";
    c=" ";
    setWindowTitle(tr("计算器"));
    ledt=new QLineEdit("0");
    ledt->setFont(QFont("Timers" , 28 ,  QFont::Bold));
    ledt->setGeometry(0,0,220,50);
    ledt->setReadOnly(1);
    hex=new QPushButton(tr("HEX"));
    dec=new QPushButton(tr("DEC "));
    bin=new QPushButton(tr("BIN"));
    lhex=new QLineEdit(this);
    ldec=new QLineEdit(this);
    lbin=new QLineEdit(this);

    maxcd=new QPushButton(tr("最大公约数"));
    squ=new QPushButton(tr("X²"));
    cube=new QPushButton(tr("^"));
    sin=new QPushButton(tr("sin"));
    cos=new QPushButton(tr("cos"));
    tan=new QPushButton(tr("tan"));
    mincm=new QPushButton(tr("最大公倍数"));
    sqr=new QPushButton(tr("√"));
    tenx=new QPushButton(tr("10^"));
    log=new QPushButton(tr("log"));
    ln=new QPushButton(tr("ln"));
    mod=new QPushButton(tr("Mod"));
    pi=new QPushButton(tr("π"));
    rec=new QPushButton(tr("1/x"));
    ce=new QPushButton(tr("CE"));
    cle=new QPushButton(tr("C"));
    back=new QPushButton(tr("←"));
    div=new QPushButton(tr("÷"));
    btn_a=new QPushButton(tr("A"));
    btn_b=new QPushButton(tr("B"));
    btn_7=new QPushButton(tr("7"));
    btn_8=new QPushButton(tr("8"));
    btn_9=new QPushButton(tr("9"));
    mul=new QPushButton(tr("×"));
    btn_c=new QPushButton(tr("C"));
    btn_d=new QPushButton(tr("D"));
    btn_4=new QPushButton(tr("4"));
    btn_5=new QPushButton(tr("5"));
    btn_6=new QPushButton(tr("6"));
    sub=new QPushButton(tr("-"));
    btn_e=new QPushButton(tr("E"));
    btn_f=new QPushButton(tr("F"));
    btn_1=new QPushButton(tr("1"));
    btn_2=new QPushButton(tr("2"));
    btn_3=new QPushButton(tr("3"));
    add=new QPushButton(tr("+"));
    leftbr=new QPushButton(tr("("));
    rightbr=new QPushButton(tr(")"));
    mark=new QPushButton(tr("±"));
    btn_0=new QPushButton(tr("0"));
    point=new QPushButton(tr("."));
    equal=new QPushButton(tr("="));

    QHBoxLayout *Hl1=new QHBoxLayout;
    QHBoxLayout *Hl2=new QHBoxLayout;
    QHBoxLayout *Hl3=new QHBoxLayout;
    QHBoxLayout *Hl4=new QHBoxLayout;
    QHBoxLayout *Hl5=new QHBoxLayout;
    QHBoxLayout *Hl6=new QHBoxLayout;
    QHBoxLayout *Hl7=new QHBoxLayout;
    QHBoxLayout *Hl8=new QHBoxLayout;
    QHBoxLayout *Hl9=new QHBoxLayout;
    QHBoxLayout *Hl10=new QHBoxLayout;
    Hl1->addWidget(hex);
    Hl1->addWidget(lhex);
    Hl2->addWidget(dec);
    Hl2->addWidget(ldec);
    Hl3->addWidget(bin);
    Hl3->addWidget(lbin);
    Hl4->addWidget(maxcd);
    Hl4->addWidget(squ);
    Hl4->addWidget(cube);
    Hl4->addWidget(sin);
    Hl4->addWidget(cos);
    Hl4->addWidget(tan);
    Hl5->addWidget(mincm);
    Hl5->addWidget(sqr);
    Hl5->addWidget(tenx);
    Hl5->addWidget(log);
    Hl5->addWidget(ln);
    Hl5->addWidget(mod);
    Hl6->addWidget(pi);
    Hl6->addWidget(rec);
    Hl6->addWidget(ce);
    Hl6->addWidget(cle);
    Hl6->addWidget(back);
    Hl6->addWidget(div);
    Hl7->addWidget(btn_a);
    Hl7->addWidget(btn_b);
    Hl7->addWidget(btn_7);
    Hl7->addWidget(btn_8);
    Hl7->addWidget(btn_9);
    Hl7->addWidget(mul);
    Hl8->addWidget(btn_c);
    Hl8->addWidget(btn_d);
    Hl8->addWidget(btn_4);
    Hl8->addWidget(btn_5);
    Hl8->addWidget(btn_6);
    Hl8->addWidget(sub);
    Hl9->addWidget(btn_e);
    Hl9->addWidget(btn_f);
    Hl9->addWidget(btn_1);
    Hl9->addWidget(btn_2);
    Hl9->addWidget(btn_3);
    Hl9->addWidget(add);
    Hl10->addWidget(leftbr);
    Hl10->addWidget(rightbr);
    Hl10->addWidget(mark);
    Hl10->addWidget(btn_0);
    Hl10->addWidget(point);
    Hl10->addWidget(equal);

    QVBoxLayout *V1=new QVBoxLayout;
    V1->addLayout(Hl1);
    V1->addLayout(Hl2);
    V1->addLayout(Hl3);
    V1->addLayout(Hl4);
    V1->addLayout(Hl5);
    V1->addLayout(Hl6);
    V1->addLayout(Hl7);
    V1->addLayout(Hl8);
    V1->addLayout(Hl9);
    V1->addLayout(Hl10);

    connect(btn_0,SIGNAL(clicked()),this,SLOT(btn_0_clicked()));
    connect(btn_1,SIGNAL(clicked()),this,SLOT(btn_1_clicked()));
    connect(btn_2,SIGNAL(clicked()),this,SLOT(btn_2_clicked()));
    connect(btn_3,SIGNAL(clicked()),this,SLOT(btn_3_clicked()));
    connect(btn_4,SIGNAL(clicked()),this,SLOT(btn_4_clicked()));
    connect(btn_5,SIGNAL(clicked()),this,SLOT(btn_5_clicked()));
    connect(btn_6,SIGNAL(clicked()),this,SLOT(btn_6_clicked()));
    connect(btn_7,SIGNAL(clicked()),this,SLOT(btn_7_clicked()));
    connect(btn_8,SIGNAL(clicked()),this,SLOT(btn_8_clicked()));
    connect(btn_9,SIGNAL(clicked()),this,SLOT(btn_9_clicked()));
    connect(btn_a,SIGNAL(clicked()),this,SLOT(btn_a_clicked()));
    connect(btn_b,SIGNAL(clicked()),this,SLOT(btn_b_clicked()));
    connect(btn_c,SIGNAL(clicked()),this,SLOT(btn_c_clicked()));
    connect(btn_d,SIGNAL(clicked()),this,SLOT(btn_d_clicked()));
    connect(btn_e,SIGNAL(clicked()),this,SLOT(btn_e_clicked()));
    connect(btn_f,SIGNAL(clicked()),this,SLOT(btn_f_clicked()));
    connect(hex,SIGNAL(clicked()),this,SLOT(hex_clicked()));
    connect(dec,SIGNAL(clicked()),this,SLOT(dec_clicked()));
    connect(bin,SIGNAL(clicked()),this,SLOT(bin_clicked()));
    connect(maxcd,SIGNAL(clicked()),this,SLOT(maxcd_clicked()));
    connect(squ,SIGNAL(clicked()),this,SLOT(squ_clicked()));
    connect(cube,SIGNAL(clicked()),this,SLOT(cube_clicked()));
    connect(sin,SIGNAL(clicked()),this,SLOT(sin_cilcked()));
    connect(cos,SIGNAL(clicked()),this,SLOT(cos_clicked()));
    connect(tan,SIGNAL(clicked()),this,SLOT(tan_clicked()));
    connect(mincm,SIGNAL(clicked()),this,SLOT(mincm_clicked()));
    connect(sqr,SIGNAL(clicked()),this,SLOT(sqr_clicked()));
    connect(tenx,SIGNAL(clicked()),this,SLOT(tenx_clicked()));
    connect(log,SIGNAL(clicked()),this,SLOT(log_clicked()));
    connect(ln,SIGNAL(clicked()),this,SLOT(ln_clicked()));
    connect(mod,SIGNAL(clicked()),this,SLOT(mod_clicked()));
    connect(pi,SIGNAL(clicked()),this,SLOT(pi_clicked()));
    connect(rec,SIGNAL(clicked()),this,SLOT(rec_clicked()));
    connect(ce,SIGNAL(clicked()),this,SLOT(ce_clicked()));
    connect(cle,SIGNAL(clicked()),this,SLOT(cle_clicked()));
    connect(back,SIGNAL(clicked()),this,SLOT(back_clicked()));
    connect(div,SIGNAL(clicked()),this,SLOT(div_clicked()));
    connect(mul,SIGNAL(clicked()),this,SLOT(mul_clicked()));
    connect(add,SIGNAL(clicked()),this,SLOT(add_clicked()));
    connect(dec,SIGNAL(clicked()),this,SLOT(dec_clicked()));
    connect(equal,SIGNAL(clicked()),this,SLOT(equal_clicked()));
    connect(leftbr,SIGNAL(clicked()),this,SLOT(leftbr_clicked()));
    connect(rightbr,SIGNAL(clicked()),this,SLOT(right_clicked()));
    connect(mark,SIGNAL(clicked()),this,SLOT(mark_clicked()));
    connect(point,SIGNAL(clicked()),this,SLOT(point_clicked()));

    setLayout(V1);


}

Widget::~Widget()
{
    delete ui;
}

//十进制数转换为二进制
QString decToBin(QString str)
{
    QString re;
    bool ok;
    int n=str.toInt(&ok,10);
    if(n<2)
         re=QString::number(n,10);
    if(n>=2)
    {
      decToBin(QString::number(n/2,10));
      re=re.append(QString::number(n%2,10));
    }
    return re;

}
//十进制转换为16进制
QString cmpTen(int n)
{

    QString re;
    switch(n)
    {
       case 10:   re="A"; break;
       case 11:   re="B"; break;
       case 12:   re="C"; break;
       case 13:   re="D"; break;
       case 14:   re="E"; break;
       case 15:   re="F"; break;
       default:   re=QString::number(n,10);
    }
    return re;
}

QString decToHex(QString str)
{
    bool ok;
    int n=str.toInt(&ok,10);
    QString re;
    if(n<16)
        cmpTen(n);
    if(n>=16)
    {
      decToHex(QString::number(n/16,10));
      cmpTen(n%16);
    }
    return re;
}
//16进制转换为10进制
QString hexToDec(QString str)
{
    QString re;
    bool ok;
    int n=str.toInt(&ok,10);
    return QString::number(n,10);

}
//2进制转10进制
QString binToDec(QString str)
{
    QString re;
    int n;
    bool ok;
    for(int i=0;i<str.length();i++)
    {
        n+=qPow(2,i)*str.mid(str.length()-i,1).toInt(&ok,10);
    }
    re=QString::number(n,10);
    return re;

}
void Widget::hex_clicked()
{
    hex->setStyleSheet("background-color: rgb(255, 255, 0)");
    lhex->setStyleSheet("color:rgb(255,255,0)");
    lhex->setText(ledt->text());

}

void Widget::dec_clicked()
{
    dec->setStyleSheet("background-color: rgb(255, 255, 0)");
    ldec->setStyleSheet("color:rgb(255,255,0)");
    btn_a->setEnabled(false);
    btn_b->setEnabled(false);
    btn_c->setEnabled(false);
    btn_d->setEnabled(false);
    btn_e->setEnabled(false);
    btn_f->setEnabled(false);
    ldec->setText(ledt->text());



}

void Widget::bin_clicked()
{
    bin->setStyleSheet("background-color: rgb(255, 255, 0)");
    lbin->setStyleSheet("color:rgb(255,255,0)");
    btn_2->setEnabled(false);
    btn_3->setEnabled(false);
    btn_4->setEnabled(false);
    btn_5->setEnabled(false);
    btn_6->setEnabled(false);
    btn_7->setEnabled(false);
    btn_8->setEnabled(false);
    btn_9->setEnabled(false);
    pi->setEnabled(false);
    btn_a->setEnabled(false);
    btn_b->setEnabled(false);
    btn_c->setEnabled(false);
    btn_d->setEnabled(false);
    btn_e->setEnabled(false);
    btn_f->setEnabled(false);
    lbin->setText(ledt->text());
}

void Widget::btn_0_clicked()
{
    if(str2=="0")
        str2="0";
    else
    {
        str2=ledt->text();
        str2.append('0');

    }
    ledt->setText(str2);

}

void Widget::btn_1_clicked()
{
    if(str2=="0")
        str2="1";
    else
    {
        str2=ledt->text();
        str2.append('1');
    }
    ledt->setText(str2);

}

void Widget::btn_2_clicked()
{
    if(str2=="0")
        str2="2";
    else
    {
        str2=ledt->text();
        str2.append('2');
    }
    ledt->setText(str2);

}

void Widget::btn_3_clicked()
{
    if(str2=="0")
        str2="3";
    else
    {
        str2=ledt->text();
        str2.append('3');
    }
    ledt->setText(str2);

}
void Widget::btn_4_clicked()
{
    if(str2=="0")
        str2="4";
    else
    {
        str2=ledt->text();
        str2.append('4');
    }
    ledt->setText(str2);

}
void Widget::btn_5_clicked()
{
    if(str2=="0")
        str2="5";
    else
    {
        str2=ledt->text();
        str2.append('5');
    }
    ledt->setText(str2);

}

void Widget::btn_6_clicked()
{
    if(str2=="0")
        str2="6";
    else
    {
        str2=ledt->text();
        str2.append('6');
    }
    ledt->setText(str2);

}

void Widget::btn_7_clicked()
{
    if(str2=="0")
        str2="7";
    else
    {
        str2=ledt->text();
        str2.append('7');
    }
    ledt->setText(str2);

}
void Widget::btn_8_clicked()
{
    if(str2=="0")
        str2="8";
    else
    {
        str2=ledt->text();
        str2.append('8');
    }
    ledt->setText(str2);

}
void Widget::btn_9_clicked()
{
    if(str2=="0")
        str2="9";
    else
    {
        str2=ledt->text();
        str2.append('9');
    }
    ledt->setText(str2);

}
void Widget::btn_a_clicked()
{
    if(str2=="0")
        str2="A";
    else
    {
        str2=ledt->text();
        str2.append('A');
    }
    ledt->setText(str2);

}
void Widget::btn_b_clicked()
{
    if(str2=="0")
        str2="B";
    else
    {
        str2=ledt->text();
        str2.append('B');
    }
    ledt->setText(str2);

}
void Widget::btn_c_clicked()
{
    if(str2=="0")
        str2="C";
    else
    {
        str2=ledt->text();
        str2.append('C');
    }
    ledt->setText(str2);

}
void Widget::btn_d_clicked()
{
    if(str2=="0")
        str2="D";
    else
    {
        str2=ledt->text();
        str2.append('D');
    }
    ledt->setText(str2);

}
void Widget::btn_e_clicked()
{
    if(str2=="0")
        str2="E";
    else
    {
        str2=ledt->text();
        str2.append('E');
    }
    ledt->setText(str2);

}
void Widget::btn_f_clicked()
{
    if(str2=="0")
        str2="F";
    else
    {
        str2=ledt->text();
        str2.append('F');
    }
    ledt->setText(str2);

}

void Widget::mark_clicked()
{
    str2=ledt->text();
    bool ok;
    int n=str2.toInt(&ok,10);
    n=n*(-1);
    ledt->setText(QString::number(n,10));
}

void Widget::pi_clicked()
{
    str2="π";
    ledt->setText(str2);


}
void Widget::point_clicked()
{
    if(str2=="0")
        str2=".";
    else
    {
        str2=ledt->text();
        str2.append('.');
    }
    ledt->setText(str2);

}


void Widget::squ_clicked()
{
    str1=ledt->text();
    str2="0";
    c="sqr";
    ledt->setText(tr("sqr(")+str2+tr(")"));

}

void Widget::cube_clicked()
{
    str1=ledt->text();
    str2="0";
    c="^";
    ledt->setText(tr("(")+str1+tr(") ^"));
}

void Widget::sin_clicked()
{
    str1=ledt->text();
    str2="0";
    c="sin";
    ledt->setText(tr("sin(")+str1+tr(")"));
}

void Widget::cos_clicked()
{
    str1=ledt->text();
    str2="0";
    c="cos";
    ledt->setText(tr("cos(")+str1+tr(")"));

}

void Widget::tan_clicked()
{
    str1=ledt->text();
    str2="0";
    c="tan";
    ledt->setText(tr("tan(")+str1+tr(")"));
}


void Widget::sqr_clicked()
{
   str1=ledt->text();
   str2="0";
   c="sqrt";
   ledt->setText(tr("√ (")+str1+tr(")"));

}

void Widget::tenx_clicked()
{
    str1=ledt->text();
    str2="0";
    c="e";
    ledt->setText(tr("10^(")+str1+tr(")"));
}

void Widget::log_clicked()
{
    str1=ledt->text();
    str2="0";
    c="log";
    ledt->setText(tr("log(")+str1+tr(")"));
}

void Widget::ln_clicked()
{
    str1=ledt->text();
    str2="0";
    c="ln";
    ledt->setText(tr("ln(")+str1+tr(")"));
}

void Widget::mod_clicked()
{
    str1=ledt->text();
    str2="0";
    c="%";
    ledt->setText(str1+tr("%"));
}


void Widget::rec_clicked()
{
    str1=ledt->text();
    str2="0";
    c="rec";
    ledt->setText(tr("1/")+str1);
}

void Widget::ce_clicked()
{
    str2=ledt->text();
    str2="0";
    ledt->setText(str2);

}

void Widget::cle_clicked()
{
    str1="0";
    str2="0";
    c=" ";
    ledt->setText(str2);
}

void Widget::back_clicked()
{
    if(str2=="0")
        str2="0";
    else
    {
       str2=ledt->text();
       str2=str2.left(str2.length()-1);
       ledt->setText(str2);
    }
}

void Widget::div_clicked()
{
    double n,m;
    str2=ledt->text();
    if(c==" ")             //如果c为空格键，说明现在是第一次运算，那么不进行任何操作，只是将Q2复制给Q1同时保存这一次的操作符，以便下一次计算.
    {
        str1=str2;
        str2="0";
        c="/";
    }
    else
    {
        n=str1.toDouble();               //如果上一次已经有运算符，那么将Q1,Q2都转换成浮点数
        m=str2.toDouble();
        if(c=="+") n=n+m;
        if(c=="-") n=n-m;
        if(c=="*") n=n*m;
        if(c=="/") n=n/m;             //实现运算
        if(c=="^") n=qPow(n,m);
        if(c=="%") n=(int)n%(int)m;
        str1=QString::number(n,'g',10);   //再转换回字符形式
        str2="0";                         //将Q2重新初始化
        c="/";
        ledt->setText(str1);          //显示中间结果
    }
}

void Widget::mul_clicked()
{
    double n,m;
    str2=ledt->text();
    if(c==" ")
    {
        str1=str2;
        str2="0";
        c="*";
    }
    else
    {
        n=str1.toDouble();
        m=str2.toDouble();
        if(c=="+") n=n+m;
        if(c=="-") n=n-m;
        if(c=="*") n=n*m;
        if(c=="/") n=n/m;
        if(c=="^") n=qPow(n,m);
        if(c=="%") n=(int)n%(int)m;
        str1=QString::number(n,'g',10);
        str2="0";
        c="*";
        ledt->setText(str1);
    }
}

void Widget::add_clicked()
{
    double n,m;
    str2=ledt->text();
    if(c==" ")             //如果c为空格键，说明现在是第一次运算，那么不进行任何操作，只是将Q2复制给Q1同时保存这一次的操作符，以便下一次计算.
    {
        str1=str2;
        str2="0";
        c="+";
    }
    else
    {
        n=str1.toDouble();               //如果上一次已经有运算符，那么将Q1,Q2都转换成浮点数
        m=str2.toDouble();
        if(c=="+") n=n+m;
        if(c=="-") n=n-m;
        if(c=="*") n=n*m;
        if(c=="/") n=n/m;             //实现运算
        if(c=="^") n=qPow(n,m);
        if(c=="%") n=(int)n%(int)m;
        str1=QString::number(n,'g',10);   //再转换回字符形式
        str2="0";                         //将Q2重新初始化
        c="+";
        ledt->setText(str1);          //显示中间结果
    }
}

void Widget::sub_clicked()
{
    double n,m;
    str2=ledt->text();
    if(c==" ")             //如果c为空格键，说明现在是第一次运算，那么不进行任何操作，只是将Q2复制给Q1同时保存这一次的操作符，以便下一次计算.
    {
        str1=str2;
        str2="0";
        c="-";
    }
    else
    {
        n=str1.toDouble();               //如果上一次已经有运算符，那么将Q1,Q2都转换成浮点数
        m=str2.toDouble();
        if(c=="+") n=n+m;
        if(c=="-") n=n-m;
        if(c=="*") n=n*m;
        if(c=="/") n=n/m;             //实现运算
        if(c=="^") n=qPow(n,m);
        if(c=='%') n=(int)n%(int)m;
        str1=QString::number(n,'g',10);   //再转换回字符形式
        str2="0";                         //将Q2重新初始化
        c="-";
        ledt->setText(str1);          //显示中间结果
    }
}

void Widget::equal_clicked()
{
    double n,m;
    str2=ledt->text();
    if(c==" "){
        str1=str2;
        str2="0";
        ledt->setText(str1);
        c="=";
    }
    else{
        n=str1.toDouble();
        m=str2.toDouble();
        if(c=="+") n=n+m;
        if(c=="-") n=n-m;
        if(c=="*") n=n*m;
        if(c=="/") n=n/m;
        if(c=="sqr") n=qPow(n,2);
        if(c=="^") n=qPow(n,m);
        if(c=="sin") n=qSin(n);
        if(c=="cos") n=qCos(n);
        if(c=="tan") n=qTan(n);
        if(c=="sqrt") n=qSqrt(n);
        if(c=="e") n=qPow(10,n);
        if(c=="log") n=qLn(n)/qLn(10);
        if(c=="ln") n=qLn(n);
        if(c=="%") n=(int)n%(int)m;
        if(c=="rec") n=1/n;
        str1=QString::number(n,'g',10);
        str2="0";
        c=" ";
        ledt->setText(str1);
        str1="0";
    }
}


